<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
  <link rel="stylesheet" title="Default" href="http://acm.math.spbu.ru/~sk1/colorer/my.css">

  <script src="http://acm.math.spbu.ru/~sk1/colorer/highlight.js"></script>
  <script src="http://acm.math.spbu.ru/~sk1/colorer/cpp.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
</head>

<body>

<pre><code>
/**
 * Author: Sergey Kopeliovich (Burunduk30@gmail.com5
 * Date: 2013.10.18
 */

#include &lt;cctype&gt;
#include &lt;cstdio&gt;
#include &lt;cassert&gt;
#include &lt;algorithm&gt;
#include &lt;map&gt;

using namespace std;

#define forn(i, n) for (int i = 0; i &lt; (int)(n); i++)

typedef long long ll;

const int N = 8;

int n, qst, qen, ans;

struct state
{
  int x, y;
  char s[N][N + 1];

  ll hash() const
  {
    ll r = x * N + y;
    forn(i, n)
      forn(j, n)
        r = r * 239 + s[i][j];
    return r;
  }
};

const int maxQ = (int)1e5;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

state s, q[maxQ];
map &lt;ll, int&gt; u;

void add( const state &s, int d )
{
  ll h = s.hash();
  if (u.count(h))
    return;
  u[h] = d;
  q[qen++] = s, qen %= maxQ;
  if (s.s[s.y][s.x] == 'E' && ans == -1)
    ans = d;
}

int main()
{
  while (scanf("%d%d%d ", &n, &s.y, &s.x) == 3 && n)
  {
    s.y--, s.x--;
    forn(i, n)
      gets(s.s[i]);
    qst = qen = 0;
    u.clear();
    ans = -1;
    add(s, 0);
    while (ans == -1 && qst &lt; qen)
    {
      s = q[qst++], qst %= maxQ;
        
      int d = u[s.hash()] + 1;
      forn(k, 4)
      {
        int x = s.x + dx[k], y = s.y + dy[k];
        if (0 &lt;= x && x &lt; n && 0 &lt;= y && y &lt; n && s.s[y][x] != '.')
        {
          state s1 = s;
          s1.y = y, s1.x = x;
          add(s1, d);
        }
        if (isdigit(s.s[s.y][s.x]))
        {
          int len = s.s[s.y][s.x] - '0', bad = 0;
          forn(i, len)
          {
            bad |= !(0 &lt;= x && x &lt; n && 0 &lt;= y && y &lt; n && s.s[y][x] == '.');
            x += dx[k], y += dy[k];
          }
          if (!bad)
          {
            state s1 = s;
            s1.s[s.y][s.x] = '.';
            x = s.x + dx[k], y = s.y + dy[k];
            s1.y = y, s1.x = x;
            forn(i, len)
            {
              s1.s[y][x] = '#';
              x += dx[k], y += dy[k];
            }
            add(s1, d);
          }
        }
      }
    }
    if (ans == -1)
      puts("Impossible");
    else
      printf("%d\n", ans);
  }
  return 0;
}

</code></pre>

</body>
</html>

<font style="visibility:hidden">
